package com.hs.auth

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.hs.auth.databinding.ModAuthLoginFragmentBinding
import com.hs.auth.databinding.ModAuthRegisterFingerFragmentBinding
import com.hs.auth.viewmodel.LoginViewModel
import com.hs.auth.viewmodel.RegisterByFingerViewModel
import com.hs.common.dto.SysUserDto
import com.hs.framework.toast.TipsToast
import dagger.hilt.android.AndroidEntryPoint

/**
 * 指纹注册的
 */
@AndroidEntryPoint
class RegisterByFingerDialogFragment  constructor(val authListener: AuthListener,val userDto: SysUserDto)  : DialogFragment() {

    private var _binding: ModAuthRegisterFingerFragmentBinding? = null

    lateinit var registerByFingerViewModel: RegisterByFingerViewModel


    companion object {
        fun show(fragmentManager: FragmentManager, userDto: SysUserDto,authListener: AuthListener) {
            val fragment = RegisterByFingerDialogFragment(authListener,userDto)
            fragment.show(fragmentManager, RegisterByFingerDialogFragment::class.java.simpleName)
        }
    }

    /**
     *
     */
    public interface AuthListener{
        fun onAuthResult(sysUserDto: SysUserDto)
    }


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.window?.setFlags(
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        )
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        return dialog
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = ModAuthRegisterFingerFragmentBinding.inflate(inflater, container, false)
        initView()
        return _binding!!.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        registerByFingerViewModel  = ViewModelProvider(this).get(RegisterByFingerViewModel::class.java)

        registerByFingerViewModel.openUsbDev()

//        registerByFingerViewModel.registerByFinger()
        observeData()

    }

    private fun observeData() {

        registerByFingerViewModel.liveData.observe(this){
            println("${it}")
            if (it != null){
                println("注册指纹成功：${it}")
                registerByFingerViewModel.updateByFingerId(it,userDto)
            }else{
                TipsToast.showTips("注册指纹失败:ID00")
            }
        }

        registerByFingerViewModel.errorLiveData.observe(this){
            _binding?.tipMsgTv?.text = it
        }

        registerByFingerViewModel.imageLiveData.observe(this){
            Glide.with(this@RegisterByFingerDialogFragment).asBitmap()
                .load(it)
                .placeholder(R.mipmap.mod_auth_ic_fp_bg)
                .error(R.mipmap.mod_auth_ic_fp_bg)
                .into(_binding!!.icFpBg)
        }

        registerByFingerViewModel.liveUpdateData.observe(this){
            if (it>0){
                if (authListener != null){
                    authListener.onAuthResult(userDto)
                }
                dismiss()
            }else{
                TipsToast.showTips("注册指纹失败:更新用户信息失败")
            }
        }

    }

    private fun initView() {
        _binding!!.icDialogClose.setOnClickListener{
            dismiss()
        }

    }

    override fun onDestroyView() {
        super.onDestroyView()

        registerByFingerViewModel.stopUsb()

        registerByFingerViewModel.stopUsbDev()

        _binding = null
    }


}